package com.seeyon.apps.common.utils;

import com.seeyon.ctp.organization.po.OrgLevel;
import com.seeyon.ctp.organization.po.OrgMember;
import com.seeyon.ctp.organization.po.OrgPost;
import com.seeyon.ctp.organization.po.OrgUnit;
import com.seeyon.ctp.util.DBAgent;
import com.seeyon.ctp.util.SQLWildcardUtil;
import com.seeyon.ocip.common.org.OrgDepartment;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrgDataUtils {

    /**
     * @MethodName departmentConvertToIdString
     * @Description
     * @param: dataList
     * @return: java.lang.String
     * @Author CheungHowrAn
     * @Date 2024/12/14 17:51
     */
    public String departmentConvertToIdString(List<OrgDepartment> dataList) {
        StringBuilder sb = new StringBuilder();
        for (OrgDepartment data : dataList) {
            sb.append(data.getId()).append(",");
        }
        return StringUtils.removeEnd(sb.toString(), ",");
    }

    /**
     * @MethodName memberConvertToIdString
     * @Description
     * @param: dataList
     * @return: java.lang.String
     * @Author CheungHowrAn
     * @Date 2024/12/14 17:51
     */
    public String memberConvertToIdString(List<OrgMember> dataList) {
        StringBuilder sb = new StringBuilder();
        for (OrgMember data : dataList) {
            sb.append(data.getId()).append(",");
        }
        return StringUtils.removeEnd(sb.toString(), ",");
    }

    /**
     * @MethodName levelConvertToIdString
     * @Description
     * @param: dataList
     * @return: java.lang.String
     * @Author CheungHowrAn
     * @Date 2024/12/14 17:51
     */
    public String levelConvertToIdString(List<OrgLevel> dataList) {
        StringBuilder sb = new StringBuilder();
        for (OrgLevel data : dataList) {
            sb.append(data.getId()).append(",");
        }
        return StringUtils.removeEnd(sb.toString(), ",");
    }

    /**
     * @MethodName postConvertToIdString
     * @Description
     * @param: dataList
     * @return: java.lang.String
     * @Author CheungHowrAn
     * @Date 2024/12/14 17:50
     */
    public String postConvertToIdString(List<OrgPost> dataList) {
        StringBuilder sb = new StringBuilder();
        for (OrgPost data : dataList) {
            sb.append(data.getId()).append(",");
        }
        return StringUtils.removeEnd(sb.toString(), ",");
    }

    /**
     * @MethodName findMemberByCode
     * @Description
     * @param: orgAccountId
     * @param: code
     * @return: java.util.List<com.seeyon.ctp.organization.po.OrgMember>
     * @Author CheungHowrAn
     * @Date 2024/12/14 17:23
     */
    public static List<OrgMember> findMemberByCode(String orgAccountId, String code) {
        StringBuilder hql = new StringBuilder();
        HashMap<String, Object> params = new HashMap();
        hql.append("SELECT m ");
        hql.append(" FROM OrgMember as m");
        hql.append(" WHERE ");
        hql.append(" m.internal=1 AND m.deleted=0 AND m.admin=0 AND m.virtual=0 AND m.assigned=1 AND m.enable=1 ");
        hql.append(" AND m.orgAccountId=:orgAccountId");
        hql.append(" AND m.code=:code ");
        params.put("orgAccountId", Long.valueOf(orgAccountId));
        params.put("code", code);
        List<OrgMember> find = DBAgent.find(hql.toString(), params);
        return find;
    }

    /**
     * @MethodName findMemberByName
     * @Description
     * @param: orgAccountId
     * @param: name
     * @return: java.util.List<com.seeyon.ctp.organization.po.OrgMember>
     * @Author CheungHowrAn
     * @Date 2024/12/14 17:23
     */
    public static List<OrgMember> findMemberByName(String orgAccountId, String name) {
        StringBuilder hql = new StringBuilder();
        HashMap<String, Object> params = new HashMap();
        hql.append("SELECT m ");
        hql.append(" FROM OrgMember as m");
        hql.append(" WHERE ");
        hql.append(" m.internal=1 AND m.deleted=0 AND m.admin=0 AND m.virtual=0 AND m.assigned=1 AND m.enable=1 ");
        hql.append(" AND m.orgAccountId=:orgAccountId");
        hql.append(" AND m.code=:code ");
        params.put("orgAccountId", Long.valueOf(orgAccountId));
        params.put("name", name);
        List<OrgMember> find = DBAgent.find(hql.toString(), params);
        return find;
    }

    /**
     * @MethodName findOrgLevelByCode
     * @Description
     * @param: orgAccountId
     * @param: code
     * @return: java.util.List<com.seeyon.ctp.organization.po.OrgUnit>
     * @Author CheungHowrAn
     * @Date 2024/12/14 17:24
     */
    public static List<OrgLevel> findOrgLevelByCode(String orgAccountId, String code) {
        StringBuilder hql = new StringBuilder();
        HashMap<String, Object> params = new HashMap();
        hql.append("SELECT m ");
        hql.append(" FROM OrgLevel as m");
        hql.append(" WHERE ");
        hql.append(" m.enable=1 AND m.deleted=0 ");
        hql.append(" AND m.orgAccountId=:orgAccountId");
        hql.append(" AND m.code=:code ");
        hql.append(" AND m.type='Department' ");
        params.put("orgAccountId", Long.valueOf(orgAccountId));
        params.put("code", code);
        List<OrgLevel> find = DBAgent.find(hql.toString(), params);
        return find;
    }

    /**
     * @MethodName findOrgLevelByName
     * @Description
     * @param: orgAccountId
     * @param: code
     * @return: java.util.List<com.seeyon.ctp.organization.po.OrgUnit>
     * @Author CheungHowrAn
     * @Date 2024/12/14 17:24
     */
    public static List<OrgLevel> findOrgLevelByName(String orgAccountId, String name) {
        StringBuilder hql = new StringBuilder();
        HashMap<String, Object> params = new HashMap();
        hql.append("SELECT m ");
        hql.append(" FROM OrgLevel as m");
        hql.append(" WHERE ");
        hql.append(" m.enable=1 AND m.deleted=0 ");
        hql.append(" AND m.orgAccountId=:orgAccountId");
        hql.append(" AND m.code=:code ");
        hql.append(" AND m.type='Department' ");
        params.put("orgAccountId", Long.valueOf(orgAccountId));
        params.put("name", name);
        List<OrgLevel> find = DBAgent.find(hql.toString(), params);
        return find;
    }

    /**
     * @MethodName findOrgPostByCode
     * @Description
     * @param: orgAccountId
     * @param: code
     * @return: java.util.List<com.seeyon.ctp.organization.po.OrgUnit>
     * @Author CheungHowrAn
     * @Date 2024/12/14 17:24
     */
    public static List<OrgPost> findOrgPostByCode(String orgAccountId, String code) {
        StringBuilder hql = new StringBuilder();
        HashMap<String, Object> params = new HashMap();
        hql.append("SELECT m ");
        hql.append(" FROM OrgPost as m");
        hql.append(" WHERE ");
        hql.append(" m.enable=1 AND m.deleted=0 ");
        hql.append(" AND m.orgAccountId=:orgAccountId");
        hql.append(" AND m.code=:code ");
        hql.append(" AND m.type='Department' ");
        params.put("orgAccountId", Long.valueOf(orgAccountId));
        params.put("code", code);
        List<OrgPost> find = DBAgent.find(hql.toString(), params);
        return find;
    }

    /**
     * @MethodName findOrgPostByName
     * @Description
     * @param: orgAccountId
     * @param: name
     * @return: java.util.List<com.seeyon.ctp.organization.po.OrgUnit>
     * @Author CheungHowrAn
     * @Date 2024/12/14 17:24
     */
    public static List<OrgPost> findOrgPostByName(String orgAccountId, String name) {
        StringBuilder hql = new StringBuilder();
        HashMap<String, Object> params = new HashMap();
        hql.append("SELECT m ");
        hql.append(" FROM OrgPost as m");
        hql.append(" WHERE ");
        hql.append(" m.enable=1 AND m.deleted=0 ");
        hql.append(" AND m.orgAccountId=:orgAccountId");
        hql.append(" AND m.code=:code ");
        hql.append(" AND m.type='Department' ");
        params.put("orgAccountId", Long.valueOf(orgAccountId));
        params.put("name", name);
        List<OrgPost> find = DBAgent.find(hql.toString(), params);
        return find;
    }

    /**
     * @MethodName findDepartmentByCode
     * @Description
     * @param: orgAccountId
     * @param: code
     * @return: java.util.List<com.seeyon.ctp.organization.po.OrgUnit>
     * @Author CheungHowrAn
     * @Date 2024/12/14 17:24
     */
    public static List<OrgUnit> findDepartmentByCode(String orgAccountId, String code) {
        StringBuilder hql = new StringBuilder();
        HashMap<String, Object> params = new HashMap();
        hql.append("SELECT m ");
        hql.append(" FROM OrgUnit as m");
        hql.append(" WHERE ");
        hql.append(" m.enable=1 AND m.deleted=0 AND m.internal=1 ");
        hql.append(" AND m.orgAccountId=:orgAccountId");
        hql.append(" AND m.code=:code ");
        hql.append(" AND m.type='Department' ");
        params.put("orgAccountId", Long.valueOf(orgAccountId));
        params.put("code", code);
        List<OrgUnit> find = DBAgent.find(hql.toString(), params);
        return find;
    }

    /**
     * @MethodName findDepartmentByName
     * @Description
     * @param: orgAccountId
     * @param: name
     * @return: java.util.List<com.seeyon.ctp.organization.po.OrgUnit>
     * @Author CheungHowrAn
     * @Date 2024/12/14 17:24
     */
    public static List<OrgUnit> findDepartmentByName(String orgAccountId, String name) {
        StringBuilder hql = new StringBuilder();
        HashMap<String, Object> params = new HashMap();
        hql.append("SELECT m ");
        hql.append(" FROM OrgUnit as m");
        hql.append(" WHERE ");
        hql.append(" m.enable=1 AND m.deleted=0 AND m.internal=1 ");
        hql.append(" AND m.orgAccountId=:orgAccountId");
        hql.append(" AND m.code=:code ");
        hql.append(" AND m.type='Department' ");
        params.put("orgAccountId", Long.valueOf(orgAccountId));
        params.put("name", name);
        List<OrgUnit> find = DBAgent.find(hql.toString(), params);
        return find;
    }

    /**
     * @MethodName findDepartmentByCode
     * @Description
     * @param: orgAccountId
     * @param: code
     * @return: java.util.List<com.seeyon.ctp.organization.po.OrgUnit>
     * @Author CheungHowrAn
     * @Date 2024/12/14 17:24
     */
    public static List<OrgUnit> findAccountByCode(String code) {
        StringBuilder hql = new StringBuilder();
        HashMap<String, Object> params = new HashMap();
        hql.append("SELECT m ");
        hql.append(" FROM OrgUnit as m");
        hql.append(" WHERE ");
        hql.append(" m.enable=1 AND m.deleted=0 AND m.internal=1 ");
        hql.append(" AND m.code=:code ");
        hql.append(" AND m.type='Account' ");
        params.put("code", code);
        List<OrgUnit> find = DBAgent.find(hql.toString(), params);
        return find;
    }

    /**
     * @MethodName findDepartmentByName
     * @Description
     * @param: orgAccountId
     * @param: name
     * @return: java.util.List<com.seeyon.ctp.organization.po.OrgUnit>
     * @Author CheungHowrAn
     * @Date 2024/12/14 17:24
     */
    public static List<OrgUnit> findAccountByName(String name) {
        StringBuilder hql = new StringBuilder();
        HashMap<String, Object> params = new HashMap();
        hql.append("SELECT m ");
        hql.append(" FROM OrgUnit as m");
        hql.append(" WHERE ");
        hql.append(" m.enable=1 AND m.deleted=0 AND m.internal=1 ");
        hql.append(" AND m.code=:code ");
        hql.append(" AND m.type='Account' ");
        params.put("name", name);
        List<OrgUnit> find = DBAgent.find(hql.toString(), params);
        return find;
    }

    /**
     * @MethodName getAllMemberPOByDepartmentId
     * @Description
     * @param: departmentId
     * @param: type
     * @param: isInternal
     * @param: enable
     * @param: condition
     * @param: fieldValue
     * @return: java.util.List<com.seeyon.ctp.organization.po.OrgMember>
     * @Author CheungHowrAn
     * @Date 2024/12/14 17:24
     */
    public static List<OrgMember> getAllMemberPOByDepartmentId(Long departmentId, Integer type, Boolean isInternal, Boolean enable, String condition, Object fieldValue) {

        StringBuilder hql = new StringBuilder();
        Map<String, Object> params = new HashMap();
        hql.append("SELECT m ");
        if ("loginName".equals(condition)) {
            hql.append(" FROM OrgMember as m, OrgPrincipal as p ");
            hql.append(" WHERE m.id=p.memberId AND ");
        } else {
            hql.append(" FROM OrgMember as m");
            hql.append(" WHERE ");
        }

        hql.append(" m.virtual=0 AND m.assigned=1");
        hql.append(" AND m.orgDepartmentId=:departmentId");
        params.put("departmentId", departmentId);
        if (type != null) {
            hql.append(" AND m.type=:type");
            params.put("type", type);
        }

        if (isInternal != null) {
            hql.append(" AND m.internal=:internal");
            params.put("internal", isInternal);
        }

        if (enable != null) {
            hql.append(" AND m.enable=:enable");
            params.put("enable", enable ? 1 : 0);
        }

        if (StringUtils.isNotBlank(condition) && !"null".equals(condition)) {
            if ("loginName".equals(condition)) {
                hql.append(" AND p.loginName LIKE :fieldValue" + SQLWildcardUtil.setEscapeCharacter());
                Object fieldValue2 = "%" + SQLWildcardUtil.escape(String.valueOf(fieldValue)) + "%";
                params.put("fieldValue", fieldValue2);
            } else {
                if (fieldValue instanceof String) {
                    hql.append(" and (").append(condition).append(" LIKE :fieldValue" + SQLWildcardUtil.setEscapeCharacter() + ")");
                    fieldValue = "%" + SQLWildcardUtil.escape(String.valueOf(fieldValue)) + "%";
                } else {
                    hql.append(" AND ").append(condition).append("=:fieldValue");
                }

                params.put("fieldValue", fieldValue);
            }
        }

        hql.append(" ORDER BY m.sortId ASC,m.name ASC");
        return DBAgent.find(hql.toString(), params);
    }


}
